﻿using System;
using System.Data;
using System.Text;
using System.IO;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace JumbotCms.Install
{
    public partial class _step1_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Application["CMS:BasePath"] == null)
                Response.Redirect("index.aspx");
            if (!Page.IsPostBack)
            {
                if (!Jumbot.Common.DirFile.FileExists("scripts/sqlserver/step1.sql"))
                {
                    this.DivMessage.InnerHtml = "脚本文件install/scripts/sqlserver/step1.sql不存在";
                    this.btnSave.Enabled = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string dbConnString = "";
            string dbConnStr = "server=" + this.txtServerName.Text + ";database=" + this.txtDatabaseName.Text + ";uid=" + this.txtUserName.Text + ";pwd=" + this.txtUserPass.Text + ";";
            dbConnString = dbConnStr;
            if (ConnOK(dbConnString))
            {
                //保存配置文件
                try
                {
                    string _connFile = Server.MapPath("~/_data/config/conn.config");
                    string _connText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><dbType>1</dbType><dbPath></dbPath><dbConnStr>" + dbConnStr + "</dbConnStr></Root>";
                    StreamWriter sw = new StreamWriter(_connFile, false, Encoding.UTF8);
                    sw.Write(_connText);
                    sw.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (Jumbot.Common.ExecuteSqlBlock.Go("1", dbConnString, Server.MapPath("scripts/sqlserver/step1.sql")))
                    Response.Redirect("step2.aspx");
                else
                    this.DivMessage.InnerHtml = "安装失败";
            }
            else
                this.DivMessage.InnerHtml = "Sql Server数据库配置有误";
            System.Web.HttpContext.Current.Application.Lock();
            System.Web.HttpContext.Current.Application["Database:dbType"] = null;
            System.Web.HttpContext.Current.Application["Database:dbConnStr"] = null;
            System.Web.HttpContext.Current.Application["System:Info"] = null;
            System.Web.HttpContext.Current.Application.UnLock();
        }
        private bool ConnOK(string connectionString)
        {
            try
            {
                SqlConnection conn1 = new SqlConnection(connectionString);
                conn1.Open();
                conn1.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
